//
//  walletViewController.m
//  ChinaA
//
//  Created by Mxionlly on 2018/11/2.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "walletViewController.h"
#import "walletTableViewCell.h"
#import "taskOverviewService.h"
#import "walletDetailViewController.h"

@interface walletViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)NSString        *burseType;
@property (nonatomic,assign)NSInteger       page;
@property (nonatomic,strong)NSMutableArray  *dataArray;
@end

@implementation walletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.available == 0) {
        self.title = @"可用钱包记录";
        self.burseType =@"20110001";
    }else if (self.available == 1){
        self.title = @"冻结钱包记录";
        self.burseType =@"20110002";
    }else{
        self.title = @"分期冻结钱包记录";
        self.burseType =@"20110003";
    }
    [self.view addSubview:self.tableView];
    self.dataArray = [NSMutableArray new];
    [self loadData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}

-(void)loadData{
    self.page = 1;
    [self.dataArray removeAllObjects];
    [self loadlistData];
}

-(void)loadMoreData{
    self.page ++;
    [self loadlistData];
}

-(void)loadlistData{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.burseType value:@"burseType"]; //钱包类型 可用钱包：20110001 冻结钱包：20110002 分期冻结钱包：20110003
    [dict setField:[NSString stringWithFormat:@"%ld",self.page] value:@"pageindex"];
    [dict setField:@"10" value:@"pagesize"];
    [taskOverviewService getUserBurseList:dict success:^(dataSourcedataUserBurseList *response, BOOL success) {
        if (self.page == 1) {
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if (success) {
            if (self.page == 1) {
                self.dataArray = response.data;
                [self.tableView reloadData];
                [self.tableView.mj_footer resetNoMoreData];
            }else{
                NSMutableArray *array = response.data;
                if (array.count > 0) {
                    [self.dataArray addObjectsFromArray:array];
                    [self.tableView reloadData];
                }else{
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

#pragma mark - loadtableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, distanceHeight, KScreenWidth, KScreenHeight-distanceHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.userInteractionEnabled = YES;
    }
    return _tableView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 50)];
    view.backgroundColor = [UIColor clearColor];
    UILabel *lbtitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 150, 50)];
    lbtitle.text = @"全部记录";
    lbtitle.font = [UIFont systemFontOfSize:14];
    [view addSubview:lbtitle];
    return view;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    walletTableViewCell *cell = [walletTableViewCell cellWithTableView:tableView];
    if (self.dataArray.count > 0) {
        dataUserBurseInfo *model  = [dataUserBurseInfo mj_objectWithKeyValues:self.dataArray[indexPath.row]];
        cell.lbCoinNumber.text = [NSString stringWithFormat:@"%@ china-A",model.CoinNumber];
        cell.lbtime.text = model.CreateTime;
        if ([model.CoinNumber floatValue] > 0) {
            cell.leftImage.image = [UIImage imageNamed:@"icon_in"];
        }else{
            cell.leftImage.image = [UIImage imageNamed:@"icon_out"];
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count > 0) {
        dataUserBurseInfo *model  = [dataUserBurseInfo mj_objectWithKeyValues:self.dataArray[indexPath.row]];
        walletDetailViewController *detail = [walletDetailViewController new];
        detail.burseid = model.Id;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
